<?php

namespace App\Http\Controllers\Admin;

use App\CodeResponse;
use App\Models\Slide;
use App\Services\SlideService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;

class SlideController extends AdminController
{
    //添加
    public function  add(Request $request){

        $validator = Validator::make($request->all(), [
            'title' => 'required|string',
            'pic' => 'required|string',
            'link' => 'required|string',
            'mobile' => 'required|integer',
            'isopen' => 'required|integer',

        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        Slide::create($request->all());
        return $this->success();
    }
    // 上传
    public function upfile(Request $request) {
        $url = config('filesystems.disks.slide.url');
        $default = config('filesystems.disks.slide.default');
        $pic =  SlideService::getInstance()->upfile($request,$url,$default,'slide');
        return $this->success([
                'pic'=>$pic,
            ]
        );
    }
    //修改
    public function  update(Request $request){
        $validator = Validator::make($request->all(), [
            'id' => 'required|integer',
            'title' => 'required|string',
            'pic' => 'required|string',
            'link' => 'required|string',
            'mobile' => 'required|integer',
            'isopen' => 'required|integer',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $putData = $request->except(['_method','_token']);
        $slide = new Slide();
        $slide->find($request->id)->update($putData);
        return $this->success();
    }
    //列表
    public function  list(Request $request){
        $validator = Validator::make($request->all(), [
            'page' => 'integer',
            'limit' => 'integer',
            'sort' => 'string',
            'order' => 'string',
            'title' => 'string',
            'isopen' => 'integer',
            'mobile' => 'integer',

        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $title = $request->input('title');
        $isopen = $request->input('isopen');
        $mobile = $request->input('mobile');
        $page = $request->input('page',1);
        $limit = $request->input('limit',10);
        $sort = $request->input('sort','sorting');
        $order = $request->input('order','asc');
        $list = SlideService::getInstance()->listSlide($page,$limit,$sort,$order, $title,$isopen,$mobile);
        return $this->success($list);
    }
    //所有幻灯片
    public function  listAll(Request $request){
        $validator = Validator::make($request->all(), [
            'page' => 'integer',
            'limit' => 'integer',
            'sort' => 'string',
            'order' => 'string',
            'title' => 'string',
            'isopen' => 'integer',
            'mobile' => 'integer',

        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $title = $request->input('title');
        $isopen = $request->input('isopen');
        $mobile = $request->input('mobile');
        $sort = $request->input('sort','created_at');
        $order = $request->input('order','asc');
        $list = SlideService::getInstance()->listAll($sort,$order, $title,$isopen,$mobile);
        return $this->success($list);
    }
    //删除
    public function  delete(Request $request){
        $validator = Validator::make($request->all(), [
            'id' => 'required|integer',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $slide = Slide::find($request->id);
        $slide->delete();
        return $this->success();
    }


}
